package com.curator.component;


import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import org.reflections.Reflections;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 构建测点策略工厂
 *
 * @author Jun
 * @since 2023/3/9
 */
public class ComponentInstallFactory {

    public static List<AbstractComponentInstall> strategyPositionBuildList = new ArrayList<>();

    static {
        // 指定包名
        Reflections reflections = new Reflections("com.curator.component");
        Set<Class<? extends AbstractComponentInstall>> implementations = reflections.getSubTypesOf(AbstractComponentInstall.class);
        for (Class<? extends AbstractComponentInstall> implementation : implementations) {
            strategyPositionBuildList.add(ReflectUtil.newInstance(implementation));
        }
    }

    /**
     * 获取安装策略
     *
     * @return {@link AbstractComponentInstall}
     */
    public AbstractComponentInstall getInstallStrategy(String componentName) {
        return strategyPositionBuildList.stream().filter(d -> StrUtil.equalsIgnoreCase(d.componentName(), componentName))
                .findFirst().orElse(null);
    }
}
